Shader "Skybox/CubemapDayNight"
{
	Properties
	{
		[NoScaleOffset]_Tex("Cubemap Day(HDR)", CUBE) = "black" {}
		[NoScaleOffset]_TexNight("Cubemap Night(HDR)", CUBE) = "black" {}
		[NoScaleOffset]_TexDusk("Cubemap Dusk(HDR)", CUBE) = "black" {}
		_Exposure("Cubemap Exposure", Range(0 , 8)) = 1
		[Gamma]_TintColor("Cubemap Tint Color", Color) = (0.5,0.5,0.5,1)
		_CubemapPosition("Cubemap Position", Float) = 0
		_DayToNight("Day To Night", Range(0, 3)) = 1

		[Space(10)]
		[Toggle(_ENABLEROTATION_ON)] _EnableRotation("Enable Rotation", Float) = 0
		[IntRange]_Rotation("Rotation", Range(0 , 360)) = 0
		_RotationSpeed("Rotation Speed", Float) = 1

		[Space(10)]
		[Toggle(_ENABLEFOG_ON)] _EnableFog("Enable Fog", Float) = 0
			//[StyledMessage(Info, The fog color is controlled by the fog color set in the Lighting panel., _EnableFog, 1, 10, 0)]_FogMessage("# FogMessage", Float) = 0
			_FogIntensity("Fog Intensity", Range(0 , 1)) = 1
			_FogHeight("Fog Height", Range(0 , 1)) = 1
			_FogSmoothness("Fog Smoothness", Range(0.01 , 1)) = 0.01
			_FogFill("Fog Fill", Range(0 , 1)) = 0.5
			_FogPosition("Fog Position", Float) = 0

	}

		SubShader
		{


			Tags { "RenderType" = "Background" "Queue" = "Background" }
		LOD 0

			CGINCLUDE
			#pragma target 2.0
			ENDCG
			Blend Off
			AlphaToMask Off
			Cull Off
			ColorMask RGBA
			ZWrite Off
			ZTest LEqual



			Pass
			{
				Name "Unlit"
				Tags { "LightMode" = "ForwardBase" }
				CGPROGRAM

				#ifndef UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX
				#define UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input)
				#endif
				#pragma vertex vert
				#pragma fragment frag
				#include "UnityCG.cginc"
				#include "UnityShaderVariables.cginc"
				#pragma shader_feature_local _ENABLEFOG_ON
				#pragma shader_feature_local _ENABLEROTATION_ON


			struct appdata
			{
				float4 vertex : POSITION;
				float4 color : COLOR;
			};

			struct v2f
			{
				float4 vertex : SV_POSITION;
				float4 texcoord1 : TEXCOORD1;
				float4 texcoord2 : TEXCOORD2;
				UNITY_VERTEX_OUTPUT_STEREO
			};

			uniform half _FogMessage;
			uniform half4 _Tex_HDR;
			uniform half _Rotationn;
			uniform samplerCUBE _Tex;
			uniform samplerCUBE _TexNight;
			uniform samplerCUBE _TexDusk;
			uniform float _CubemapPosition;
			uniform float _DayToNight;
			uniform half _Rotation;
			uniform half _RotationSpeed;
			uniform half4 _TintColor;
			uniform half _Exposure;
			uniform float _FogPosition;
			uniform half _FogHeight;
			uniform half _FogSmoothness;
			uniform half _FogFill;
			uniform half _FogIntensity;

			v2f vert(appdata v)
			{
				v2f o;
				UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);

				float lerpResult268 = lerp(1.0 , (unity_OrthoParams.y / unity_OrthoParams.x) , unity_OrthoParams.w);
				half CAMERA_MODE300 = lerpResult268;
				float3 appendResult1220 = (float3(v.vertex.xyz.x , (v.vertex.xyz.y * CAMERA_MODE300) , v.vertex.xyz.z));
				float3 appendResult1208 = (float3(0.0 , -_CubemapPosition , 0.0));
				half3 VertexPos40_g1 = appendResult1220;
				float3 appendResult74_g1 = (float3(0.0 , VertexPos40_g1.y , 0.0));
				float3 VertexPosRotationAxis50_g1 = appendResult74_g1;
				float3 break84_g1 = VertexPos40_g1;
				float3 appendResult81_g1 = (float3(break84_g1.x , 0.0 , break84_g1.z));
				float3 VertexPosOtherAxis82_g1 = appendResult81_g1;
				half Angle44_g1 = (1.0 - radians((_Rotation + (_Time.y * _RotationSpeed))));
				#ifdef _ENABLEROTATION_ON
				float3 staticSwitch1164 = ((VertexPosRotationAxis50_g1 + (VertexPosOtherAxis82_g1 * cos(Angle44_g1)) + (cross(float3(0,1,0) , VertexPosOtherAxis82_g1) * sin(Angle44_g1))) + appendResult1208);
				#else
				float3 staticSwitch1164 = (appendResult1220 + appendResult1208);
				#endif
				float3 vertexToFrag774 = staticSwitch1164;
				o.texcoord1.xyz = vertexToFrag774;

				o.texcoord2 = v.vertex;

				o.texcoord1.w = 0;
				float3 vertexValue = float3(0, 0, 0);
				o.vertex = UnityObjectToClipPos(v.vertex);

				return o;
			}

			fixed4 RGB2CMYK(fixed3 col) {
				fixed K = 1 - max(col.r, max(col.g, col.g));
				fixed C = (1 - col.r - K) / (1 - K);
				fixed M = (1 - col.g - K) / (1 - K);
				fixed Y = (1 - col.b - K) / (1 - K);
				return fixed4(C, M, Y, K);
			}

			fixed3 CMYK2RGB(fixed4 cmyk) {
				fixed C = cmyk.x;
				fixed M = cmyk.y;
				fixed Y = cmyk.z;
				fixed K = cmyk.w;
				fixed R = (1 - C) * (1 - K);
				fixed G = (1 - M) * (1 - K);
				fixed B = (1 - Y) * (1 - K);
				return fixed3(R, G, B);
			}

			fixed4 frag(v2f i) : SV_Target
			{
				UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
				fixed4 finalColor;
				float3 texcoord = i.texcoord1.xyz;
				half4 colorDay = texCUBE(_Tex, texcoord);
				half4 colorNight = texCUBE(_TexNight, texcoord);
				half4 colorDusk = texCUBE(_TexDusk, texcoord);
				half4 colorSky;
				if (_DayToNight < 1)
					colorSky = lerp(colorNight, colorDay, _DayToNight);
				else if (_DayToNight < 2)
				{
					_DayToNight = _DayToNight - 1;
					//colorSky = colorDay * (1-_DayToNight) + colorDusk * (_DayToNight);// 
					fixed4 cmykDay = RGB2CMYK(colorDay);
					fixed4 cmykDusk = RGB2CMYK(colorDusk);
					colorSky.rgb = CMYK2RGB(lerp(cmykDay, cmykDusk, _DayToNight));
					//colorSky = lerp(colorDay, colorDusk, _DayToNight);
					//colorSky = lerp(colorDay, colorDusk, _DayToNight - 1);
				}
				else
					colorSky = lerp(colorDusk, colorNight, _DayToNight - 2);

				//colorSky = lerp(colorDay, colorDusk, 0.75);
				colorSky.xyz = DecodeHDR(colorSky, _Tex_HDR);
				colorSky = half4(colorSky.xyz, 0) * unity_ColorSpaceDouble * _TintColor * _Exposure;

				#ifdef _ENABLEFOG_ON
					float fogMask = lerp(saturate(pow((0.0 + (abs((i.texcoord2.xyz.y + -_FogPosition)) - 0.0) * (1.0 - 0.0) / (_FogHeight - 0.0)) , (1.0 - _FogSmoothness))) , 0.0 , _FogFill);
					fogMask = lerp(1.0 , fogMask, _FogIntensity);
					finalColor = lerp(unity_FogColor, colorSky, fogMask);
				#else
					finalColor = colorSky;
				#endif

				return finalColor;
			}
			ENDCG
		}
		}

			Fallback "Skybox/Cubemap"
}
